# Maintainer: George Rawlinson <george@rawlinson.net.nz>
# Contributor: graysky <graysky AT archlinux DOT us>
# Contributor: Stéphane Graber <stgraber AT ubuntu DOT com>

pkgname=distrobuilder
pkgver=2.1
pkgrel=2
pkgdesc='System container image builder for LXC and LXD'
arch=('loong64' 'x86_64')
url='https://github.com/lxc/distrobuilder'
license=('Apache')
depends=(
  'rsync'
  'squashfs-tools'
  'gnupg'
  'debootstrap'
  'dosfstools'
  'gptfdisk'
  'qemu-img'
  'e2fsprogs'
  'btrfs-progs'
)
makedepends=('go' 'git')
optdepends=(
  'cdrtools: for repack-windows command'
  'hivex: for repack-windows command'
  'wimlib: for repack-windows command'
)
source=("$url/releases/download/$pkgname-$pkgver/$pkgname-$pkgver.tar.gz"{,.asc})
sha512sums=('80184d27305f5659f1367563161220b966b1957810da61e8f3b9f32cf408795319b43dd650bc9f867b3ac73ef2b3251972bb11999d92feb34bfd9cdae7ff7b7b'
            'SKIP')
b2sums=('90e82a20a3ef61d13148951956bb1f74cc3494ca86e339c188a1c9009dcb27c0cb9a298c639c6de891df67915b48d107557f6b698a45cbe35c54d41a45bac1ab'
        'SKIP')
validpgpkeys=('602F567663E593BCBD14F338C638974D64792D67') # Stéphane Graber <stgraber@stgraber.org>

prepare() {
  cd "$pkgname-$pkgver"

  # create folder for build output
  mkdir build
}

build() {
  cd "$pkgname-$pkgver"

  # set Go flags
  export CGO_CPPFLAGS="$CPPFLAGS"
  export CGO_CFLAGS="$CFLAGS"
  export CGO_CXXFLAGS="$CXXFLAGS"
  export CGO_LDFLAGS="$LDFLAGS"
  export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=vendor -modcacherw"

  # verify modules
  go mod verify

  # build binary
  go build -v -o build ./...
}

check() {
  cd "$pkgname-$pkgver"
  go test -v ./...
  go vet -v ./...
}

package() {
  install -vDm755 -t "$pkgdir/usr/bin" "$pkgname-$pkgver/build/$pkgname"
}
